package com.meihua.baoleyi.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.dialog.ClearCacheDialog;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.utils.ToastUtil;
import com.meihua.baoleyi.widget.PasswordEditText;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 添加银行卡验证密码
 * Created by liming on 2017/10/18.
 */
public class AddBankVerifyPwdActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.text_title)
    TextView tvTitle;

    private PasswordEditText add_bank_verify_pwd_et;//密码接收
    private TextView verify_pwd_tv_one;//密码显示框1
    private TextView verify_pwd_tv_two;//密码显示框2
    private TextView verify_pwd_tv_three;//密码显示框3
    private TextView verify_pwd_tv_four;//密码显示框4
    private TextView verify_pwd_tv_five;//密码显示框5
    private TextView verify_pwd_tv_six;//密码显示框6

    //    private BaseMessageDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bank_verify_pwd);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText("验证密码");
        verify_pwd_tv_one = (TextView) findViewById(R.id.verify_pwd_tv_one);
        verify_pwd_tv_two = (TextView) findViewById(R.id.verify_pwd_tv_two);
        verify_pwd_tv_three = (TextView) findViewById(R.id.verify_pwd_tv_three);
        verify_pwd_tv_four = (TextView) findViewById(R.id.verify_pwd_tv_four);
        verify_pwd_tv_five = (TextView) findViewById(R.id.verify_pwd_tv_five);
        verify_pwd_tv_six = (TextView) findViewById(R.id.verify_pwd_tv_six);
        add_bank_verify_pwd_et = (PasswordEditText) findViewById(R.id.add_bank_verify_pwd_et);
        add_bank_verify_pwd_et.addTextChangedListener(textWatcher);
        /*判断是否设置过支付密码*/
        hasSetPayPassword();
    }

    private void hasSetPayPassword() {
        TreeMap<String, String> params = new TreeMap() {{
            put("user_id", myApp.getInstance().getUser_id());//用户id
        }};
        NetHelper.get(APIServer.ISSETPAYPASSWORD, params, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, okhttp3.Response response) {
                JSONObject obj = null;
                try {
                    obj = new JSONObject(s);
                    if (obj.getInt("code") == 200 && obj.getInt("is_set_pay") == 1) {
                        //未设置
                          /*去设置支付密码*/
                        ClearCacheDialog dlg = new ClearCacheDialog.Builder(AddBankVerifyPwdActivity.this)
                                .setMessage("您还未设置支付密码~前往设置密码界面？")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        Intent intent = new Intent(AddBankVerifyPwdActivity.this, SetPayPwdActivity.class);
                                        intent.putExtra("type", 5);
                                        startActivity(intent);
                                        dialogInterface.dismiss();
                                        finish();
                                    }
                                })
                                .setNativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        dialogInterface.dismiss();
                                        finish();
                                    }
                                })
                                .build();
                        dlg.show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    ToastUtil.show(AddBankVerifyPwdActivity.this, "服务器异常");
                }
            }
        });

    }


    /**
     * 文本监听事件
     */
    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            if (add_bank_verify_pwd_et.getText().toString().length() > 0) {
                verify_pwd_tv_one.setText("1");
            } else {
                verify_pwd_tv_one.setText("");
            }
            if (add_bank_verify_pwd_et.getText().toString().length() > 1) {
                verify_pwd_tv_two.setText("1");
            } else {
                verify_pwd_tv_two.setText("");
            }
            if (add_bank_verify_pwd_et.getText().toString().length() > 2) {
                verify_pwd_tv_three.setText("1");
            } else {
                verify_pwd_tv_three.setText("");
            }
            if (add_bank_verify_pwd_et.getText().toString().length() > 3) {
                verify_pwd_tv_four.setText("1");
            } else {
                verify_pwd_tv_four.setText("");
            }
            if (add_bank_verify_pwd_et.getText().toString().length() > 4) {
                verify_pwd_tv_five.setText("1");
            } else {
                verify_pwd_tv_five.setText("");
            }
            if (add_bank_verify_pwd_et.getText().toString().length() > 5) {
                verify_pwd_tv_six.setText("1");
                verifyPayPwd(add_bank_verify_pwd_et.getText().toString().trim());
            } else {
                verify_pwd_tv_six.setText("");
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

    /*密码输入完毕*/
    private void verifyPayPwd(final String pwd) {
        showDialog();
        TreeMap<String, String> params = new TreeMap() {{
            put("pay_password", pwd);//密码
            put("user_id", myApp.getInstance().getUser_id());//用户id
        }};
        NetHelper.post(APIServer.VERIFYPAYPASSWORD, params, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, okhttp3.Response response) {
                dismissDialog();
                JSONObject obj = null;
                try {
                    obj = new JSONObject(s);
                    if (obj.getInt("code") == 200) {
                        //请求成功
                        Toast.makeText(AddBankVerifyPwdActivity.this, "验证成功", Toast.LENGTH_SHORT).show();
                        Intent in = new Intent(AddBankVerifyPwdActivity.this, AddBankInfoActivity.class);
                        finish();
                        startActivity(in);
                        finish();
                    } else {
                        Toast.makeText(AddBankVerifyPwdActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    ToastUtil.show(AddBankVerifyPwdActivity.this, "服务器异常");
                }
            }
        });
    }

    @OnClick(R.id.text_back)
    public void onViewClicked() {
        finish();
    }

}